const { app, BrowserWindow, Menu, Tray ,Notification, ipcMain, nativeImage} = require('electron')
const Task = require('./utils/task');
const MSG_TYPE = {
  RULES_UPDATE: 'RULES_UPDATE',
  CONFIG_UPDATE: 'CONFIG_UPDATE'
}
let contextMenu=null;
let tray = null
app.on('ready', ()  => {
  tray = new Tray(nativeImage.createFromPath(`${app.getAppPath()}/resources/icons/png/16x16.png`))
  contextMenu = Menu.buildFromTemplate([
    { label: 'Turn Stock On',
      type: 'radio',
      click: function() {
        handlerContextMenu('TURN_ON');
      }
    },
    { label: 'Turn Stock Off',
      type: 'radio',
      click: function() {
        handlerContextMenu('TURN_OFF');
      }
    },
    { label: '规则',
      type: 'normal',
      click: function() {
        handlerContextMenu('RULE');
      },
    },
    { label: '设置', 
      type: 'normal' ,
      click: function() {
        handlerContextMenu('CONFIG');
      },
    },
    { label: '支持', 
      type: 'normal',
      click: function() {
        handlerContextMenu('SUPPORT');
      },
    },
    { label: '关于我们',
      type: 'normal',
      click: function() {
        handlerContextMenu('US');
      },
    },
  ]);
  tray.setToolTip('Stock cat 给你不一样的体验！');
  tray.setContextMenu(contextMenu);
  Task.reload();
})


function handlerContextMenu(type) {
  if (type === 'TURN_ON') {
    Task.turnOnRuleCheck();
    if (win != null) {
      win.webContents.send('CONFIG_UPDATE', type);
    }
    contextMenu.items[1].enabled = true
    contextMenu.items[0].enabled = false
    return;
  } else if (type === 'TURN_OFF') {
    Task.turnOffRuleCheck();
    contextMenu.items[1].enabled = false
    contextMenu.items[0].enabled = true
    if (win != null) {
      win.webContents.send('CONFIG_UPDATE', type);
    }
    return;
  }

  if (win === null) {
    createWindow(() => {
      win.webContents.send('CONTEXT_MENU', type);
    });
  } else {
    win.show();
    win.webContents.send('CONTEXT_MENU', type);
  }
  
}

let win
function createWindow (callback=null) {
  win = new BrowserWindow({ width: 500, height: 400,webPreferences: {nodeIntegration: true},resizable: false,show: false})
  win.loadFile('index.html')
  // win.webContents.openDevTools()
  win.on('closed', () => {
    win = null
  });
  win.on('ready-to-show', () => {
    win.show();
  })
  win.webContents.once('dom-ready', () => {
    if (callback && typeof callback === 'function') {
      callback();
    }
  });
}

app.on('ready', createWindow)

// 当全部窗口关闭时退出。
app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    app.quit()
  }
})

app.on('activate', () => {
  if (win === null) {
    createWindow()
  }
})

// 规则更新
ipcMain.on(MSG_TYPE.RULES_UPDATE, () => {
  Task.reload();
});

// 配置更新
ipcMain.on(MSG_TYPE.CONFIG_UPDATE, () => {
  Task.reload();

  if(Task.CONFIG.status == 'on') {
    contextMenu.items[1].enabled = true
    contextMenu.items[0].enabled = false
  } else {
    contextMenu.items[1].enabled = false
    contextMenu.items[0].enabled = true
  }

});
